<template>
  <a-form ref="formRef" :model="formState">
    <div class="flex justify-end mb-2">
      <a-button class="mr-2" @click="onPrintDiagnosis">打印诊断证明书</a-button>
      <a-button @click="onPrintHoliday">打印休假证明书</a-button>
    </div>
    <a-form-item name="content" label="建议">
      <a-textarea v-model:value="formState.content" style="height: calc(100vh - 400px)" placeholder="请输入" />
    </a-form-item>
    <div class="flex justify-end mt-4">
      <a-button class="mr-4" @click="formState.content = ''">重置</a-button>
      <a-button type="primary" :disabled="!formState.content" @click="onSubmit">保存</a-button>
    </div>
  </a-form>
</template>
<script setup>
import { onMounted, ref } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useStore from '../store'

const store = useStore()
const formRef = ref(null)
const formState = ref({})

const getData = async () => {
  const res = await post('/outp/mzys/getOutpatientCertificate', {
    brId: store.currentPatient.brId,
    content: formState.value.content,
    jzbh: store.currentPatient.jzbh
  })
  if (res.code !== 0) return message.error(res.msg)
  formState.value.content = res.data?.content
  formState.value.id = res.data?.id
}

const onSubmit = async () => {
  const res = await post('/outp/mzys/saveOutpatientCertificate', {
    brId: store.currentPatient.brId,
    content: formState.value.content,
    jzbh: store.currentPatient.jzbh,
    id: formState.value.id || undefined
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}

const onPrintDiagnosis = async () => {
  const { code, msg, data } = await post('/outp/outPrint/getOutpatientCertificateDocuments', {
    mzjlbh: store.currentPatient.jzbh,
    pjhm: store.currentPatient.pjhm
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

const onPrintHoliday = async () => {
  const { code, msg, data } = await post('/outp/outPrint/getOutpatientFurloughDocuments', {
    mzjlbh: store.currentPatient.jzbh,
    pjhm: store.currentPatient.pjhm
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
onMounted(() => {
  getData()
})
</script>
